package org.saasplay.resource;

import org.saasplay.resolver.XmlResolverUtils;

import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cuishifeng
 */
public class XmlDataServiceResource  implements ServiceResource {

    private Map<String, ServiceData> pageServiceDataMap = new ConcurrentHashMap<>(16);
    private String configPath;

    public XmlDataServiceResource(String configPath) {
        this.configPath = configPath;
    }

    public XmlDataServiceResource(URL url) {
        this.configPath = url.getPath();
    }

    @Override
    public ServiceData getServiceData(String pageCode) throws Exception {
        if (!this.pageServiceDataMap.isEmpty()) {
            return this.pageServiceDataMap.get(pageCode);
        }
        Map<String, ServiceData> serviceDataMap = XmlResolverUtils.read(this.configPath);
        if (serviceDataMap == null && serviceDataMap.isEmpty()) {
            throw new RuntimeException("service data must is not null or empty!");
        }
        this.pageServiceDataMap = serviceDataMap;
        return this.pageServiceDataMap.get(pageCode);
    }

    public Map<String, ServiceData> getServiceDataMap() {
        return this.pageServiceDataMap;
    }

    public String getConfigPath() {
        return configPath;
    }

}
